package com.ruoyi.common.utils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Random;

public class FileUtils {
    private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
    private static final Random RANDOM = new Random(1000);

    /**
     * 生成唯一文件名（避免重名）
     * @param originalFileName 原始文件名（包含扩展名）
     * @return 唯一文件名（如：20231020153045_123.jpg）
     */
    public static String generateUniqueFileName(String originalFileName) {
        String extension = originalFileName.substring(originalFileName.lastIndexOf("."));
        String timestamp = FORMATTER.format(LocalDateTime.now());
        int randomNum = RANDOM.nextInt(1000);
        return timestamp + "_" + randomNum + extension;
    }
}